package com.jk.smartelderlycare.controller;

import com.jk.smartelderlycare.config.RedisCache;
import com.jk.smartelderlycare.dto.SysUser;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.security.LoginUser;
import com.jk.smartelderlycare.util.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Api(tags = "用户登录模块")
@CrossOrigin
@RestController
public class LoginController {

    //定义了认证Authentication的方法
    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisCache redisCache;

    @ApiOperation(value = "用户登录", httpMethod = "POST", notes = "用户登录")
    @PostMapping(value = "/user/login")
    public ResponseResult login(@RequestBody SysUser sysUser) {
        //创建token令牌
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(sysUser.getUserName(), sysUser.getPassword());
        //调用认证方法
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        if(Objects.isNull(authenticate)) {
            throw new RuntimeException("用户名或密码错误");
        }
        //获得当前登录人的用户信息和权限集合
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String userId = loginUser.getUser().getUserId().toString();
        String token = JwtUtils.createJwt(userId);
        //authenticate存入redis
        redisCache.setCacheObject("login:"+userId, loginUser);

        //把token响应给前端
        Map<String, Object> params = new HashMap<>();
        params.put("token", token);

        return new ResponseResult(200, "登录成功", params);
    }

    @ApiOperation(value = "用户退出", httpMethod = "GET", notes = "用户退出")
    @RequestMapping(value = "/user/logout")
    public ResponseResult logout() {
        //获得当前登录人
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //删除缓存中的数据
        Long userId = loginUser.getUser().getUserId();
        redisCache.deleteObject("login:" + userId);
        return new ResponseResult(200, "退出成功！");
    }

//    @RequestMapping(value = "/user/isExistUser")
//    public ResponseResult isExistUser() {
//        //1.获得当前登录人
//        String userId = SecurityContextHolder.getContext().getAuthentication().getName();
//        if ("anonymousUser".equals(userId)) { //未登录
//            return new ResponseResult(444, "用户未登录");
//        } else {
//            return new ResponseResult(200, "用户已登录");
//        }
//    }
}
